package cn.edu.xmu.oomall.customer.controller;


import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.model.vo.PageVo;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import cn.edu.xmu.oomall.customer.controller.vo.CouponVo;
import cn.edu.xmu.oomall.customer.dao.bo.Coupon;
import cn.edu.xmu.oomall.customer.service.CouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController /*Restful的Controller对象*/
@RequestMapping(produces = "application/json;charset=UTF-8")
@RequiredArgsConstructor
@Slf4j
public class CouponController {

    private final CouponService couponService;

    /**
     * 顾客领取优惠券
     *
     * @param id
     * @param user
     * @return
     */
    @PostMapping("/couponactivities/{id}/coupons")
    @Audit(departName = "customer")
    public ReturnObject claimCoupon(@PathVariable Long id,
                                    @RequestParam Long customerId,
                                    @LoginUser UserDto user
    ) {
        this.couponService.claimCoupon(id, customerId, user);
        return new ReturnObject();
    }

    /**
     * 顾客查询优惠券
     * @param used
     * @param user
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/coupons")
    @Audit(departName = "customer")
    public ReturnObject retrieveCoupons(@RequestParam Long used,
                                       @LoginUser UserDto user,
                                       @RequestParam(required = false, defaultValue = "1") Integer page,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        List<Coupon> couponList = couponService.retrieveCoupons(used,user, page, pageSize);
        List<CouponVo> couponVos = couponList.stream()
                .map(bo -> CloneFactory.copy(new CouponVo(bo), bo))
                .collect(java.util.stream.Collectors.toList());
        return new ReturnObject(new PageVo<>(couponVos, page, pageSize));
    }
}
